package com.npjscg.medicalpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.controller.dto.FormDTO;
import com.npjscg.medicalpro.controller.dto.ReportDTO;
import com.npjscg.medicalpro.entity.Check;
import com.npjscg.medicalpro.entity.Report;
import com.npjscg.medicalpro.mapper.ReportMapper;
import com.npjscg.medicalpro.service.IReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raote
 * @since 2022-06-29
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements IReportService {

    @Resource
    private ReportMapper reportMapper;

    @Autowired
    private CheckServiceImpl checkService;

    @Override
    public IPage getByStatus(Page<ReportDTO> page, int uid, int status, String temname) {

        temname = "%" +temname +"%";
        return reportMapper.getByStatus(page,uid,status,temname);
    }

    @Override
    public Boolean updateForm(Report report) {
        UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", report.getId());
        update(report, updateWrapper);

        UpdateWrapper<Check> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("rid", report.getId());
        updateWrapper1.set("msg", "已重新编辑，请管理员重新审核");
        updateWrapper1.set("status",0);
        checkService.update(updateWrapper1);
        return true;
    }

    @Override
    public FormDTO getDTO(Integer id) {

        return reportMapper.getDTO(id);
    }

    @Override
    public void fillForm(Report report) {

        reportMapper.saveReport(report);
        checkService.addCheck(report);
    }
}
